package camelinaction;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 会话管理者
 * @author -琴兽-
 *
 */
public class SessionManager {

	/**
	 * 在线会话
	 */
	private static final ConcurrentHashMap<Long, ChatSession> onlineSessions = new ConcurrentHashMap<>();
	
	/**
	 * 加入
	 * @param playerId
	 * @param
	 * @return
	 * putIfAbsent方法主要是在向ConcurrentHashMap中添加键—值对的时候，它会先判断该键值对是否已经存在。
	 *
	 * 如果不存在（新的entry），那么会向map中添加该键值对，并返回null。
	 * 如果已经存在，那么不会覆盖已有的值，直接返回已经存在的值。
	 */
	public static boolean putSession(long playerId, ChatSession session){
		if(!onlineSessions.containsKey(playerId)){
			boolean success = onlineSessions.putIfAbsent(playerId, session)== null? true : false;
			return success;
		}
		return false;
	}
	
	/**
	 * 移除
	 * @param playerId
	 */
	public static ChatSession removeSession(long playerId){
		return onlineSessions.remove(playerId);
	}
	
	/**
	 * 发送消息[自定义协议]
	 * @param <T>
	 * @param playerId
	 * @param message
	 */
	public static <T > void sendMessage(long playerId, short module, short cmd, T message){
		ChatSession session = onlineSessions.get(playerId);
		if (session != null && session.isConnected()) {
			//Response response = new Response(module, cmd, message);
			//ResponseWithDomian responseWithDomian=new ResponseWithDomian(module, cmd, message);
			ChatResponse chatResponse=(ChatResponse)message;
			ObjectMapper objectMapper=new ObjectMapper();
			objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
			byte[] data=null;
			try {
				data=objectMapper.writeValueAsBytes(chatResponse);
			}catch (Exception ex){
				System.out.println(ex);
			}


			Response response=new Response(module,cmd,data);
			session.write(response);
		}
	}
	/**
	 * 发送消息[自定义协议]
	 * @param <T>
	 * @param playerId
	 * @param message
	 */
	public static <T extends Serializer> void sendMessage(long playerId, short module, short cmd, T message){
		ChatSession session = onlineSessions.get(playerId);
		if (session != null && session.isConnected()) {
			Response response = new Response(module, cmd, message.getBytes());
			session.write(response);
		}
	}
	/**
	 * 发送消息[自定义协议]
	 * @param <T>
	 * @param playerId
	 * @param message
	 */
/*	public static <T*//* extends Serializer*//*> void sendMessage(long playerId, short module, short cmd, T message){
		ChatSession session = onlineSessions.get(playerId);
		if (session != null && session.isConnected()) {
			Response response = new Response(module, cmd, message.getBytes());
			//Response response = new Response(module, cmd, new byte[2]);
			session.write(response);
		}
	}*/
	/**
	 * 发送消息[自定义协议]
	 * @param <T>
	 * @param playerId
	 * @param message
	 */
	public static  void sendMessage(long playerId, ChatResponse chatResponse){
		ChatSession session = onlineSessions.get(playerId);
		if (session != null && session.isConnected()) {
			//Response response = new Response(module, cmd, message.getBytes());
			//Response response = new Response(module, cmd, new byte[2]);

			session.write(chatResponse);

		}
	}
	public static  void sendMessage(long playerId, short module, short cmd, String message){
		ChatSession session = onlineSessions.get(playerId);
		if (session != null && session.isConnected()) {
			Response response = new Response(module, cmd, message.getBytes());
			//Response response = new Response(module, cmd, new byte[2]);
			session.write(response);

		}
	}
	/**
	 * 发送消息[自定义协议]
	 * @param <T>
	 * @param playerId
	 * @param message
	 */
	public static  void sendMessage(long playerId, Result<?> result){
		ChatSession session = onlineSessions.get(playerId);
		if (session != null && session.isConnected()) {
			//Response response = new Response(module, cmd, message.getBytes());
			//Response response = new Response(module, cmd, new byte[2]);

			session.write(result);

		}
	}
	/**
	 * 发送消息[protoBuf协议]
	 * @param <T>
	 * @param playerId
	 * @param message
	 *//*
	public static <T extends GeneratedMessage> void sendMessage(long playerId, short module, short cmd, T message){
		ChatSession session = onlineSessions.get(playerId);
		if (session != null && session.isConnected()) {
			Response response = new Response(module, cmd, message.toByteArray());
			session.write(response);
		}
	}
	*/
	/**
	 * 是否在线
	 * @param playerId
	 * @return
	 */
	public static boolean isOnlinePlayer(long playerId){
		return onlineSessions.containsKey(playerId);
	}
	
	/**
	 * 获取所有在线玩家
	 * @return
	 */
	public static Set<Long> getOnlinePlayers() {
		return Collections.unmodifiableSet(onlineSessions.keySet());
	}
}
